//
//  GFDetialTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFDetialTableViewCell.h"

@interface GFDetialTableViewCell ()

@property (nonatomic, strong) UIView *detialBackgroundView;
@property (nonatomic, strong) UIImageView *userImage;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *userTag;
@property (nonatomic, strong) UILabel *foodName;
@property (nonatomic, strong) UIImageView *foodImage;

@end

@implementation GFDetialTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor colorWithRed:0.841 green:0.814 blue:0.860 alpha:1.000];
        
        self.detialBackgroundView = [[UIView alloc] init];
        self.detialBackgroundView.backgroundColor = [UIColor whiteColor];
        self.detialBackgroundView.layer.cornerRadius = 5;
        self.detialBackgroundView.layer.masksToBounds = YES;

        
        [self.contentView addSubview:self.detialBackgroundView];
        
        self.userImage = [[UIImageView alloc] init];
        self.userImage.backgroundColor = [UIColor grayColor];
        self.userImage.layer.cornerRadius = ScreenWidth / 20;
        self.userImage.layer.masksToBounds = YES;
        [self.detialBackgroundView addSubview:self.userImage];
        
        self.userNameLabel = [[UILabel alloc] init];
        self.userNameLabel.font = [UIFont systemFontOfSize:17];
        self.userNameLabel.textColor = [UIColor colorWithRed:1.000 green:0.448 blue:0.636 alpha:0.780];
        [self.detialBackgroundView addSubview:self.userNameLabel];
        
        self.userTag = [[UILabel alloc] init];
        self.userTag.layer.cornerRadius = 5;
        self.userTag.layer.masksToBounds = YES;
        self.userTag.font = [UIFont systemFontOfSize:13];
        self.userTag.textAlignment = NSTextAlignmentCenter;
        self.userTag.textColor = [UIColor whiteColor];
        [self.detialBackgroundView addSubview:self.userTag];
        
        self.foodName = [[UILabel alloc] init];
        self.foodName.font = [UIFont systemFontOfSize:13];
        [self.detialBackgroundView addSubview:self.foodName];
        
        self.foodImage = [[UIImageView alloc] init];
        self.foodImage.image = [UIImage imageNamed:@"category_1"];
        self.foodImage.userInteractionEnabled = YES;
        [self.detialBackgroundView addSubview:self.foodImage];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [tap addTarget:self action:@selector(tap:)];
        [self.foodImage addGestureRecognizer:tap];
        
    }
    return self;
}

-(void)tap:(UITapGestureRecognizer *)tap {
    
    self.block();
    
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    self.detialBackgroundView.frame = CGRectMake(10, 0, ScreenWidth - 20, ScreenWidth - 20);
    
    self.userImage.frame = CGRectMake(10, 10, ScreenWidth / 10, ScreenWidth / 10);
    
    CGRect width = [self.model.user_name boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.userNameLabel.frame = CGRectMake(10 + ScreenWidth / 10 + 10, 10, width.size.width, ScreenWidth / 20);
    
    CGRect widthTag = [self.model.level_title boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.userTag.frame = CGRectMake(20 + ScreenWidth / 10 + 10 + width.size.width, 10, widthTag.size.width, ScreenWidth / 20);
    
    self.foodName.frame = CGRectMake(10 + ScreenWidth / 10 + 10, ScreenWidth / 20 + 10, ScreenWidth - 10 - ScreenWidth / 10 -30, ScreenWidth / 20);
    
    self.foodImage.frame = CGRectMake(10, ScreenWidth / 10 + 20, ScreenWidth - 40, ScreenWidth - 100);
    
    
}

-(void)setModel:(GFDetialModel *)model {
    
    if (_model != model) {
        _model = model;
    }
    
    if (model) {
        [self.userImage setImageWithURL:[NSURL URLWithString:model.user_image] placeholderImage:[UIImage imageNamed:@"category_1"]];
        self.userNameLabel.text = model.user_name;
        self.foodName.text = [model.food_name stringByAppendingFormat:@"@%@", model.place_name];
        [self.foodImage setImageWithURL:[NSURL URLWithString:model.picture_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
        if ([model.level_title isEqualToString:@"路人"]) {
            self.userTag.backgroundColor = [UIColor brownColor];
        } else if ([model.level_title isEqualToString:@"食客"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.089 green:0.409 blue:1.000 alpha:1.000];
        } else if ([model.level_title isEqualToString:@"美食爱好者"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.926 green:0.248 blue:0.845 alpha:1.000];
        } else if ([model.level_title isEqualToString:@"美食评论员"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.227 green:0.926 blue:0.249 alpha:1.000];
        } else if ([model.level_title isEqualToString:@"美食专家"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.050 green:0.267 blue:0.078 alpha:1.000];
        }
        self.userTag.text = model.level_title;
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
